﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.UnitiveDocument.NewDoc
{
    /// <summary>
    /// Listview 的摘要说明。
    /// </summary>
    public partial class ListViewPage : Page
    {
        protected override void OnInit(EventArgs e)
        {
            btnThowAwayDocument.Click += btnThowAwayDocument_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["SortField"] = "DocAddedDate";
                ViewState["SortDirect"] = "ASC";

                Bangding();
                //this.btnThowAwayDocument.Attributes["onclick"] = "javascript:return confirm('您确认要丢弃吗?');";
            }

            base.OnLoad(e);
        }

        public string GetRealName(string Username)
        {
            if (Username != "")
            {
                return Components.Staff.GetRealNameByUsername(Username);
            }
            return "";
        }

        private string GetSelectedItemID(string controlID)
        {
            string selectedID = "";
            //遍历DataGrid获得checked的ID
            foreach (DataGridItem item in dgDocList.Items)
            {
                if (((CheckBox)item.FindControl(controlID)).Checked)
                {
                    selectedID += dgDocList.DataKeys[item.ItemIndex] + ",";
                }
            }
            if (selectedID.Length > 0)
            {
                selectedID = selectedID.Substring(0, selectedID.Length - 1);
            }
            return selectedID;
        }

        private void btnThowAwayDocument_Click(object sender, EventArgs e)
        {
            var myDocument = new DocumentClass();
            String DocIDs = GetSelectedItemID("DocID");
            myDocument.DocDelete(DocIDs, 0);
            Response.Write("<Script language='javascript'>alert('文件丢弃成功！');</script>");
            Bangding();
        }

        #region 绑定DBGRID

        private void Bangding()
        {
            var myDesktop = new Components.Desktop();
            String UserName = SiteContext.Current.UserName;

            DataTable dt;
            using (SqlDataReader dr = myDesktop.GetMyDocument(UserName, 10))
            {
                dt = Tools.ConvertDataReaderToDataTable(dr);
            }

            dt.DefaultView.Sort = ViewState["SortField"] + " " + ViewState["SortDirect"];

            dgDocList.DataSource = dt.DefaultView;
            dgDocList.DataBind();
        }

        #endregion

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dgDocList.CurrentPageIndex = e.NewPageIndex;
            Bangding();
        }

        #endregion

        #region 将数据排序

        /// <summary>
        /// 将信件按照指定字段进行排序
        /// </summary>	
        public void DataGrid_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            if (ViewState["SortField"].ToString() == e.SortExpression)
            {
                ViewState["SortDirect"] = (ViewState["SortDirect"].ToString() == "ASC") ? "DESC" : "ASC";
            }
            else
            {
                ViewState["SortField"] = e.SortExpression;
                ViewState["SortDirect"] = "ASC";
            }

            foreach (DataGridColumn col in dgDocList.Columns)
            {
                if (col.SortExpression == ViewState["SortField"].ToString())
                {
                    //					if(ViewState["SortDirect"].ToString() == "ASC")
                    //					{						
                    //						col.HeaderText +="<img src='../../../images/asc.gif' border=0 />";						
                    //					}
                    //					else
                    //						col.HeaderText +="<img src='../../../images/desc.gif' border=0 />";
                }
            }

            Bangding();
        }

        #endregion
    }
}